home *** CD-ROM | disk | FTP | other *** search
- /*===================================================================*/
- /*----muProlog specifics, using a communicator */
- /*===================================================================*/
- /*---Start up the communicator, with pipe connections */
- set_up_server_communications__(Server_machine,Client_machines,
- Communications_information):-
- pipe(Read1,Write1),
- pipe(Read2,Write2),
- start_communicator__(Read1,Write1,Read2,Write2,Client_machines).
- /*-------------------------------------------------------------------*/
- /*----Start the communicator process, passing the appropriate ends of*/
- /*----the pipe in the exec */
- start_communicator__(Channel_from_communicator,Write1,Read2,
- Channel_to_communicator,Client_machines):-
- fork,
- /*----This is the server process here */
- close(Write1),
- close(Read2),
- asserta(channel_from_communicator__(Channel_from_communicator)),
- asserta(channel_to_communicator__(Channel_to_communicator)),
- Start_machines_term =.. [machines|Client_machines],
- write_term_to_communicator__(Start_machines_term).
-
- start_communicator__(Read1,Channel_to_server,Channel_from_server,
- Write2,_):-
- /*----This is the communicator process */
- close(Read1),
- close(Write2),
- get_descriptor_atom__(Channel_to_server,Write_atom),
- get_descriptor_atom__(Channel_from_server,Read_atom),
- execlp(communicator,communicator,Write_atom,Read_atom,z,z).
- /*-------------------------------------------------------------------*/
- /*----Stop the communicator by sending a message to it */
- close_down_server_communications__(_,_,_):-
- write_term_to_communicator__(exit),
- retract(channel_from_communicator__(Channel_from_communicator)),
- close(Channel_from_communicator),
- retract(channel_to_communicator__(Channel_to_communicator)),
- close(Channel_to_communicator).
- /*-------------------------------------------------------------------*/
- /*----Clients to not need any communications setup */
- /*----set_up_client_communications__(_,_,_).
- /*-------------------------------------------------------------------*/
- /*----Clients do not need any communications shutdown */
- close_down_client_communications__(_,_,_).
- /*-------------------------------------------------------------------*/
- /*----Nothing to do on power up for other machines */
- power_up_machine__(Machine).
- /*-------------------------------------------------------------------*/
- /*----Nothing to do on power down for a machine */
- power_down_machine__(Machine).
- /*-------------------------------------------------------------------*/
- /*----To start a client, send a message to the communicator. It will */
- /*----do an rexec to get the machine going, and waiting for a */
- /*----client_eval__. */
- start_client__(Machine):-
- write_term_to_communicator__(start(Machine)).
- /*-------------------------------------------------------------------*/
- /*----To stop a machine, its pipe must be closed by the communicator */
- stop_client__(Machine):-
- write_term_to_communicator__(stop(Machine)).
- /*-------------------------------------------------------------------*/
- /*----This is to send a message to a client. Simply pass to */
- /*----communicator */
- send_term_to_client__(Machine,Term):-
- debug_network__(forward(Machine,Term)),
- write_term_to_communicator__(forward(Machine,Term)).
- /*-------------------------------------------------------------------*/
- /*----For the server to receive a term, read from the communicator */
- receive_term_from_any_client__(Term):-
- debug_network__(waiting_for_term_from_client),
- read_term_from_communicator__(Term),
- debug_network__(from_communicator(Term)).
- /*-------------------------------------------------------------------*/
- /*----This gets a term from the server, when in a client. As the io */
- /*----is vis stdin and stdout, simply read a term. */
- receive_term_from_server__(Term):-
- debug_network__(waiting_for_term_from_server),
- read(A_term),
- debug_network__(from_server(A_term)),
- A_term = Term.
- /*-------------------------------------------------------------------*/
- /*----For a client to send a term to the server, simply write to */
- /*----stdout. See above for comment. */
- send_term_to_server__(Term):-
- debug_network__(to_server(Term)),
- write_term__(forward(server(1),Term)).
- /*-------------------------------------------------------------------*/
- /*----Write a term to the communicator, down the channel */
- write_term_to_communicator__(Term):-
- channel_to_communicator__(Channel_to_communicator),
- write_term_to_channel__(Channel_to_communicator,Term).
- /*-------------------------------------------------------------------*/
- /*----Read a term from the communicator channel */
- read_term_from_communicator__(Term):-
- channel_from_communicator__(Channel_from_communicator),
- /*----Because some Prologs won't allow non-variables to be read, read*/
- /*----into a new variable and instantiate afterwards */
- read(Channel_from_communicator,A_term),
- A_term = Term.
- /*-------------------------------------------------------------------*/
- /*----Writing a term must be followed by writing a . */
- write_term__(Term):-
- write(Term),
- writeln('.').
- /*-------------------------------------------------------------------*/
- /*----Writing a term must be followed by writing a . */
- write_term_to_channel__(Channel,Term):-
- write(Channel,Term),
- writeln(Channel,'.').
- /*-------------------------------------------------------------------*/
- /*----Get an atom of a file descriptor for a channel */
- get_descriptor_atom__(Channel,Atom):-
- descriptor(Channel,Descriptor),
- integer_atom__(Descriptor,Atom).
- /*-------------------------------------------------------------------*/
- /*----Convert a two digit integer to an atom */
- integer_atom__(Integer,Atom):-
- Tens is (Integer / 10) + 48,
- Ones is (Integer mod 10) + 48,
- name(Atom,[Tens,Ones]).
- /*-------------------------------------------------------------------*/
- debug_network__(Term):-
- debugging_net__,
- !,
- write(Term),
- nl.
-
- debug_network__(_).
- /*-------------------------------------------------------------------*/
-